﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CuitWinMenuItem.cs" company="">
//   
// </copyright>
// <summary>
//   The cuit win menu item.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace UITest.CUIT.CuitControls.WinControls
{
    using System.Collections.Generic;
    using System.Linq;

    using Microsoft.VisualStudio.TestTools.UITesting;
    using Microsoft.VisualStudio.TestTools.UITesting.WinControls;

    /// <summary>
    ///     The cuit win menu item.
    /// </summary>
    public class CuitWinMenuItem : CuitWinControl<WinMenuItem>
    {
        #region Constructors and Destructors

        /// <summary>
        ///     Initializes a new instance of the <see cref="CuitWinMenuItem" /> class.
        /// </summary>
        public CuitWinMenuItem()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitWinMenuItem"/> class.
        /// </summary>
        /// <param name="testControl">
        /// The test control.
        /// </param>
        public CuitWinMenuItem(UITestControl testControl)
            : base(testControl)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitWinMenuItem"/> class.
        /// </summary>
        /// <param name="testControl">
        /// The test control.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        public CuitWinMenuItem(UITestControl testControl, string searchProperties)
            : base(testControl, searchProperties)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitWinMenuItem"/> class.
        /// </summary>
        /// <param name="testControl">
        /// The test control.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        /// <param name="filterProperties">
        /// The filter properties.
        /// </param>
        public CuitWinMenuItem(UITestControl testControl, string searchProperties, string filterProperties)
            : base(testControl, searchProperties, filterProperties)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitWinMenuItem"/> class.
        /// </summary>
        /// <param name="wrappedControl">
        /// The wrapped control.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        public CuitWinMenuItem(ICuitControlBase wrappedControl, string searchProperties)
            : base(wrappedControl, searchProperties)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CuitWinMenuItem"/> class.
        /// </summary>
        /// <param name="wrappedControl">
        /// The wrapped control.
        /// </param>
        /// <param name="searchProperties">
        /// The search properties.
        /// </param>
        /// <param name="filterProperties">
        /// The filter properties.
        /// </param>
        public CuitWinMenuItem(ICuitControlBase wrappedControl, string searchProperties, string filterProperties)
            : base(wrappedControl, searchProperties, filterProperties)
        {
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets a value indicating whether checked.
        /// </summary>
        public bool Checked
        {
            get
            {
                this._control.WaitForControlReady();
                return this._control.Checked;
            }
        }

        /// <summary>
        ///     Gets the display text.
        /// </summary>
        public string DisplayText
        {
            get
            {
                return this._control.DisplayText;
            }
        }

        /// <summary>
        ///     Gets a value indicating whether has child nodes.
        /// </summary>
        public bool HasChildNodes
        {
            get
            {
                return this._control.HasChildNodes;
            }
        }

        /// <summary>
        ///     Gets a value indicating whether is top level menu.
        /// </summary>
        public bool IsTopLevelMenu
        {
            get
            {
                return this._control.IsTopLevelMenu;
            }
        }

        /// <summary>
        ///     Gets the items.
        /// </summary>
        public UITestControlCollection Items
        {
            get
            {
                this._control.WaitForControlReady();
                return this._control.Items;
            }
        }

        /// <summary>
        ///     Gets the items as cui te.
        /// </summary>
        public List<CuitWinMenuItem> ItemsAsASTControl
        {
            get
            {
                this._control.WaitForControlReady();
                var list = new List<CuitWinMenuItem>();
                foreach (WinMenuItem item in this._control.Items)
                {
                    var cuiteItem = new CuitWinMenuItem();
                    cuiteItem.WrapReady(item);
                    list.Add(cuiteItem);
                }

                return list;
            }
        }

        /// <summary>
        ///     Gets the items as list.
        /// </summary>
        public List<string> ItemsAsList
        {
            get
            {
                this._control.WaitForControlReady();
                return (from x in this._control.Items select ((WinMenuItem)x).DisplayText).ToList<string>();
            }
        }

        /// <summary>
        ///     Gets the shortcut.
        /// </summary>
        public string Shortcut
        {
            get
            {
                return this._control.Shortcut;
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///     The check.
        /// </summary>
        public void Check()
        {
            this._control.WaitForControlReady();
            if (!this._control.Checked)
            {
                this._control.Checked = true;
            }

            this.WriteTestOutput(string.Empty);
        }

        /// <summary>
        ///     The un check.
        /// </summary>
        public void UnCheck()
        {
            this._control.WaitForControlReady();
            if (this._control.Checked)
            {
                this._control.Checked = false;
            }

            this.WriteTestOutput(string.Empty);
        }

        #endregion
    }
}